package com.corpgovernment.organization.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.corpgovernment.api.organization.model.ffpinfo.CreateOrUpdateOrgUserFfpRequest;
import com.corpgovernment.api.organization.model.ffpinfo.GetOrgUserFfpRequest;
import com.corpgovernment.api.organization.model.ffpinfo.UserFfpVo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.organization.entity.db.BdFpFfcRelation;
import com.corpgovernment.organization.entity.db.MbOrgUserFfpinfo;
import com.corpgovernment.organization.entity.db.MbPassportWarnInfo;
import com.corpgovernment.organization.service.IOrganizationUserFfpService;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 常旅客controller
 *
 * @author wangyujue
 */
@Slf4j
@RestController
@RequestMapping("/ffp")
public class UserFfpController {
    @Autowired
    private IOrganizationUserFfpService organizationUserFfpService;


    /**
     * 查询用户列表的FFP信息
     *
     * @param uids 用户id列表
     * @return 航司二字码，常旅客卡号
     */
    @RequestMapping("/listByUids")
    JSONResult<List<UserFfpVo>> listByUids(@RequestParam(name = "uids") List<String> uids) {
        List<MbOrgUserFfpinfo> mbOrgUserFfpinfos = organizationUserFfpService.listByUids(uids);
        if (CollectionUtils.isEmpty(mbOrgUserFfpinfos)) {
            return new JSONResult<>(Collections.emptyList());
        }
        List<UserFfpVo> returnList = new ArrayList<>(mbOrgUserFfpinfos.size());
        for (MbOrgUserFfpinfo mbOrgUserFfpinfo : mbOrgUserFfpinfos) {
            UserFfpVo userFfpVo = new UserFfpVo();
            BeanUtils.copyProperties(mbOrgUserFfpinfo, userFfpVo);
            returnList.add(userFfpVo);
        }
        return new JSONResult<>(returnList);
    }

    /**
     * 获得用户常旅客信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/getUserFfpinfo")
    public JSONResult getUserFfpinfo(GetOrgUserFfpRequest request) {
//        Page page = organizationUserFfpinfoService.listByUid(request);
        return JSONResult.ok();
    }

    /**
     * 保存用户常旅客信息
     *
     * @param request
     * @return
     */
    @PostMapping("/saveUserFfpinfo")
    public JSONResult saveUserFfpinfo(CreateOrUpdateOrgUserFfpRequest request) {
        MbOrgUserFfpinfo mbOrgUserFfpinfo = new MbOrgUserFfpinfo();

        boolean flag = organizationUserFfpService.save(mbOrgUserFfpinfo);
        return JSONResult.success(flag);
    }

    /**
     * 检查用户ffp信息，没有保存，有则更新
     *
     * @param userFfpList 用户userFfpList
     * @return
     */
    @RequestMapping("/check")
    public JSONResult check(@RequestBody List<UserFfpVo> userFfpList) {
        log.info("检查用户ffp信息,userFfpList:{}", JsonUtils.toJsonString(userFfpList));
        for (UserFfpVo userFfpVo : userFfpList) {
            Long id = userFfpVo.getId();
            log.info("id:{}", id);
            if (id == null){
                MbOrgUserFfpinfo mbOrgUserFfpinfo = new MbOrgUserFfpinfo();
                mbOrgUserFfpinfo.setFfpno(userFfpVo.getFfpno());
                mbOrgUserFfpinfo.setAirline(userFfpVo.getAirline());
                mbOrgUserFfpinfo.setIsDeleted(false);
                mbOrgUserFfpinfo.setUid(userFfpVo.getUid());
                mbOrgUserFfpinfo.setNonEmployeeUid(userFfpVo.getNonEmployeeId());
                boolean save = organizationUserFfpService.save(mbOrgUserFfpinfo);
                log.info("save");
            }else {
                // 修改流程
                MbOrgUserFfpinfo mbOrgUserFfpinfo = new MbOrgUserFfpinfo();
                mbOrgUserFfpinfo.setFfpno(userFfpVo.getFfpno());
                mbOrgUserFfpinfo.setId(userFfpVo.getId());
                mbOrgUserFfpinfo.setAirline(userFfpVo.getAirline());
                organizationUserFfpService.update(mbOrgUserFfpinfo);
                log.info("update");
            }

        }
        return JSONResult.ok();
    }

    /**
     * 删除用户常旅客信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUserFfpinfo/{id}")
    public JSONResult deleteUserFfpinfo(@PathVariable(name = "id") Long id) {
        boolean flag = organizationUserFfpService.delete(id);
        return JSONResult.success(flag);
    }

    /**
     * 查询用户和非员工的的FFP列表信息
     *
     * @param uids           用户id列表
     * @param nonEmployeeIds 非用户id列表
     * @return 航司二字码，常旅客卡号
     */
    @RequestMapping("/listByUidsAndNonEmployeeIds")
    JSONResult<List<UserFfpVo>> listByUidsAndNonEmployeeIds(@RequestParam(name = "uids", required = false) List<String> uids, @RequestParam(name = "nonEmployeeIds", required = false) List<Long> nonEmployeeIds) {
        if (CollectionUtils.isEmpty(uids) && CollectionUtils.isEmpty(nonEmployeeIds)) {
            return JSONResult.errorMsg("参数不能都为空");
        }
        List<UserFfpVo> returnList = new ArrayList<>();
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(uids)) {
            List<MbOrgUserFfpinfo> mbOrgUserFfpinfos = organizationUserFfpService.listByUids(uids);
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(mbOrgUserFfpinfos)) {
                for (MbOrgUserFfpinfo mbOrgUserFfpinfo : mbOrgUserFfpinfos) {
                    UserFfpVo userFfpVo = new UserFfpVo();
                    BeanUtils.copyProperties(mbOrgUserFfpinfo, userFfpVo);
                    returnList.add(userFfpVo);
                }
            }
        }
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(nonEmployeeIds)) {
            List<MbOrgUserFfpinfo> nonEmployeeFfpInfos = organizationUserFfpService.listByNonEmployeeIds(nonEmployeeIds);
            if (org.apache.commons.collections.CollectionUtils.isNotEmpty(nonEmployeeFfpInfos)) {
                for (MbOrgUserFfpinfo mbOrgUserFfpinfo : nonEmployeeFfpInfos) {
                    UserFfpVo userFfpVo = new UserFfpVo();
                    BeanUtils.copyProperties(mbOrgUserFfpinfo, userFfpVo);
                    userFfpVo.setNonEmployeeId(mbOrgUserFfpinfo.getNonEmployeeUid());
                    returnList.add(userFfpVo);
                }
            }
        }

        if (!CollectionUtils.isEmpty(returnList)) {
            List<String> airlineCodes = returnList.stream().map(UserFfpVo::getAirline).collect(Collectors.toList());
            if(CollectionUtil.isEmpty(airlineCodes)){
                return new JSONResult<>(returnList);
            }
            Map<String, List<BdFpFfcRelation>> collect = organizationUserFfpService.getAirlineNameByAirlineCode(airlineCodes).stream().collect(Collectors.groupingBy(BdFpFfcRelation::getCardType));
            if(CollectionUtil.isEmpty(collect)){
                return new JSONResult<>(returnList);
            }
            for (UserFfpVo userFfpVo : returnList) {
                if(CollectionUtil.isNotEmpty(collect.get(userFfpVo.getAirline()))){
                    userFfpVo.setAirlineName(collect.get(userFfpVo.getAirline()).get(0).getCardName());
                }
            }
        }

        return new JSONResult<>(returnList);
    }

    @RequestMapping("/insertWarnInfo")
    boolean insertWarnInfo(@RequestBody MbPassportWarnInfo mbPassportWarnInfo){
        return organizationUserFfpService.insertWarnInfo(mbPassportWarnInfo);
    }

    @RequestMapping("/getNameConfirmSwitch")
    boolean getNameSwitch(){
        return organizationUserFfpService.getNameConfirmSwitch();
    }
}
